package com.oliver.picturebackendcommon.manager;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.oliver.picturebackendcommon.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CosManager
 * @Description TODO
 * @Author snow
 * @Date 2024/12/13 21:01
 **/

@Component
public class CosManager {

    @Autowired
    private CosClientConfig cosClientConfig;

    @Autowired
    private COSClient cosClient;


    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);

        //  使用腾讯云的cos的数据万象解析
        PicOperations picOperations = new PicOperations();

        //  1表示返回原图信息
        picOperations.setIsPicInfo(1);

        //  2.对图片进行压缩
        List<PicOperations.Rule> ruleList = new ArrayList<>();
        //  将图片压缩（转化为webp格式）
        String webpKey = FileUtil.mainName(key) + ".webp";
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setRule("imageMogr2/format/webp");
        compressRule.setBucket(cosClientConfig.getBucket());
        compressRule.setFileId(webpKey);
        ruleList.add(compressRule);

        //  3.缩略图处理,仅对大于 20kb 的图片生成缩略图
        if (file.length() > 2 * 1024){
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            thumbnailRule.setBucket(cosClientConfig.getBucket());
            String suffix = FileUtil.getSuffix(key);
            if (StrUtil.isBlank(suffix)){
                suffix = "jpg";
            }
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + suffix;
            thumbnailRule.setFileId(thumbnailKey);
            //  缩略规则 /thumbnail/<Width> x <Height>> (如果大于原图宽高，则不处理)
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>",600,600));
            ruleList.add(thumbnailRule);
        }

        //  构造处理图片的参数
        picOperations.setRules(ruleList);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 下载对象
     *
     * @param key
     * @return
     */
    public COSObject getObject(String key){
        COSObject object = cosClient.getObject(cosClientConfig.getBucket(), key);
        return object;
    }

    /**
     * 删除对象
     *
     * @param key 文件 key
     */
    public void deleteObject(String key) throws CosClientException {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }


}
